<?php
class BrandNewAction extends LoginAction
{
    protected $_type = 'new';
    public function index ()
    {}
    public function fb ()
    {
        $id = intval(getGet('id'));
        $floorBrand = $this->getBaseData($id);
        $mall = $this->__get('mall');
        $floor = $this->__get('floor');
        $brand = $this->__get('brand');
        $BrandNewsModel = D('BrandNews');
        $data = $BrandNewsModel->getIndexData($mall['id'], $floor['id'], $brand['id'], $floorBrand['model']);
        $this->assign('news', $data['data']);
        $this->assign('page', $data['page']);
        $this->display();
    }
    /**
     * 添加品牌资讯
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function add ()
    {
        $id = intval(getGet('id'));
        $floorBrand = $this->getBaseData($id);
        if (isPost() && $floorBrand) {
            $data = $this->getPostDate(false, $floorBrand);
            $this->createAdd('BrandNews', $data);
        }
        $this->display();
    }
    /**
     * 修改品牌资讯
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function edit ()
    {
        $id = intval(getGet('id'));
        $newId = intval(getGet('nid'));
        $map = array();
        $map['id'] = $newId;
        $new = $this->setBaseData('BrandNews', $map, 'new'); //获取资讯信息
        $floorBrand = $this->getBaseData($id);
        if (isPost()) {
            $data = $this->getPostDate(true, $floorBrand);
            $data['id'] = $newId;
            $this->createUpdate('BrandNews', $data, false);
            if ($data['image']) {
                if ($data['image'] != $new['image']) {
                    $this->delUpLoadFile($data['image'],$this->_type);
                }
            }
        }
        //获取图片
        $imageUrl = $this->getManagedUrl($new['image'], $this->_type, true);
        $this->assign('imageUrl', $imageUrl);
        $this->display();
    }
    public function getPostDate ($isEdit = false, $fb)
    {
        $data = array();
        $mall = $this->__get('mall');
        $floor = $this->__get('floor');
        $brand = $this->__get('brand');
        $user = $this->__get('userInfo');
        $data['title'] = getPost('title');
        $data['detail'] = getPost('detail');
        $data['keyword'] = getPost('keyword');
        $data['mid'] = $mall['id'];
        $data['fid'] = $floor['id'];
        $data['bid'] = $brand['id'];
        $data['editor'] = $user['id'];
        $data['model'] = $fb['model'];
        $data['start_time'] = strtotime(getPost('start_time'));
        $data['end_time'] = strtotime(getPost('end_time'));
        if ($data['start_time'] > $data['end_time']) {
            $this->error('活动结束时间不能小于开始时间！');
        }
        //文件执行上传并且保存返回媒体库标示
        $doUpload = true;
        if ($isEdit) {
            if (! $_FILES['image']['name']) {
                $doUpload = false;
            }
        }
        if ($doUpload) {
            $fId = $this->uploadFiles($this->_type, true);
            $data['image'] = $fId; //媒体库标示
            if(!$data['image']){
            	$this->error('图片传送失败');
            }
        }
        return $data;
    }
    public function getBaseData ($id)
    {
        $floorBrand = $this->getFloorBrand($id);
        if (! $floorBrand) {
            $this->error('商场楼层品牌信息不存在！');
        }
        $this->getMarket($floorBrand['mid']);
        $this->getFloor($floorBrand['fid']);
        $this->getBrand($floorBrand['bid']);
        $this->assign('fbId', $id);
        return $floorBrand;
    }
    public function getFloorBrand ($id)
    {
        return $this->setBaseData('FloorBrand', array('id' => $id), 'fb');
    }
    public function getMarket ($id)
    {
        $this->setBaseData('Market', array('id' => $id), 'mall', false, 'id,name');
    }
    public function getFloor ($id)
    {
        $this->setBaseData('Floor', array('id' => $id), 'floor', false, 'id,name,sain');
    }
    public function getBrand ($id)
    {
        $this->setBaseData('Brand', array('id' => $id), 'brand', false, 'id,name');
    }
}
?>